# Who Will Defend Democracy? Evaluating Tradeoffs in Candidate Support Among Partisan Donors and Voters
# Step 12: Estimate and plot marginal means for public sample (Appendix)
# Last updated: June 19, 2020

# Initial settings --------------------------------------------------------

library(tidyverse)
library(estimatr)
library(ggthemes)
library(ggrepel)
library(cowplot)

rm(list = ls())

source("functions/order_attributes_levels.R")
source("functions/set_my_ggtheme.R")

options(stringsAsFactors = FALSE)

# Load data ---------------------------------------------------------------

load("temp/data_for_analysis_public.RData")

Attribute_order <- c("Partisanship","Gender","Race","Voting","Investigations","Compromise","Courts","Discrimination","Taxes")
Level_order <- c("Democrat","Republican","Male","Female","White","Black","Hispanic",
                 "Opposes voter ID laws","Supports voter ID laws","Partisan involvement","Independent",
                 "Supports compromise","Stand up to other party","Obey courts","Disregard politicized decisions",
                 "Prevent discrimination","Not a big problem","Less progressive","More progressive")

df <- df_cleaned_public %>% 
  filter(!is.na(partisanship_dum_3)) %>% 
  mutate(id = group_indices(., id)) %>% 
  select(c(1,5:16), partisanship_dum_3) %>%
  mutate_if(is.factor, as.character) %>% 
  gather("attribute", "level", 5:13) %>% 
  mutate(attribute_level = paste(attribute, level, sep = ": "))

attribute_levels_only <- df %>% 
  select(attribute_level, attribute, level) %>% 
  distinct()

m0 <- df %>% 
  nest(-attribute_level) %>% 
  mutate(result = map(data, ~ lm_robust(selected ~ 1, data = .x %>% filter(partisanship_dum_3 == 1), cluster = id, se_type = "stata")),
         tidied = map(result, tidy)) %>% 
  unnest(tidied) %>% 
  as_tibble() %>% 
  select(attribute_level, estimate, p.value, conf.low, conf.high) %>% 
  mutate(panel = "Republican")

m1 <- df %>% 
  nest(-attribute_level) %>% 
  mutate(result = map(data, ~ lm_robust(selected ~ 1, data = .x %>% filter(partisanship_dum_3 == 2), cluster = id, se_type = "stata")),
         tidied = map(result, tidy)) %>% 
  unnest(tidied) %>% 
  as_tibble() %>% 
  select(attribute_level, estimate, p.value, conf.low, conf.high) %>% 
  mutate(panel = "Democrat")

diff <- df %>% 
  nest(-attribute_level) %>% 
  mutate(result = map(data, ~ lm_robust(selected ~ partisanship_dum_3, data = .x, cluster = id, se_type = "stata")),
         tidied = map(result, tidy)) %>% 
  unnest(tidied) %>% 
  as_tibble() %>%
  filter(term == "partisanship_dum_3") %>% 
  select(attribute_level, estimate, p.value, conf.low, conf.high) %>% 
  mutate(panel = "Difference")

attribute_only <- data.frame(
  attribute = rep(Attribute_order, 3),
  panel = c(rep("Republican", length(Attribute_order)),
            rep("Democrat", length(Attribute_order)),
            rep("Difference", length(Attribute_order))),
  stringsAsFactors = FALSE)

results1 <- bind_rows(m0, m1, diff) %>% 
  left_join(attribute_levels_only, by = "attribute_level") %>% 
  bind_rows(attribute_only) %>% 
  group_by(panel) %>% 
  mutate(order = order_attributes_levels(attribute, level)) %>% 
  ungroup() %>% 
  arrange(order) %>%
  mutate(var.names = paste0(attribute, ":"),
         var.names = ifelse(!is.na(level), paste0("   ", level), var.names)) %>% 
  mutate(color = 1, 
         color = ifelse(panel %in% c("Republican", "Democrat"), 2, color),
         color = ifelse(panel == "Difference" & conf.low > 0 & conf.high > 0, 3, color),
         color = ifelse(panel == "Difference" & conf.low < 0 & conf.high < 0, 3, color)) %>% 
  mutate(panel = factor(panel, 
                        levels = c("Democrat", "Republican", "Difference")))

g1 <- ggplot() +
  geom_hline(data = results1 %>% filter(panel == "Difference"), 
             mapping = aes(yintercept = 0),
             color = "gray80",
             linetype = "dashed") +
  geom_hline(data = results1 %>% filter(panel != "Difference"), 
             mapping = aes(yintercept = 0.5),
             color = "gray80",
             linetype = "dashed") +
  geom_pointrange(data = results1, 
                  mapping = aes(x = reorder(var.names, order),
                                y = estimate,
                                ymax = conf.high,
                                ymin = conf.low, 
                                color = factor(color)),
                  na.rm = TRUE) +
  facet_wrap(~ panel, scales = "free_x") +
  scale_color_manual(values = c("darkgray", "darkgray", "black")) +
  guides(color = FALSE) +
  coord_flip() +
  labs(y = "\nExpected value of outcome variable",
       x = NULL) +
  set_my_ggtheme()

g1

ggsave(paste0("figures/marginal-means-public.pdf"), 
       plot = g1, width = 12, height = 10)